import { Command } from '../constant';
import Taro from '@tarojs/taro';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import api from 'api';
import { extraPathsValue } from '@/redux/util';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    /**
     * @param tab
     */
    async changeSegment(value) {
      dispatch({
        type: Command.commonChange,
        payload: { paths: ['main', 'segmentTab'], value: value },
      });
    },
    /**
   * 改变状态类型值
   * @param tab
   */
    async changeStatus(value) {
      action.commonChange('main.form.status', value);
      action.commonChange('main.form.pageNum', 0);
      action.propPage()
    },
    /**
  * 改变宝箱状态类型值
  * @param tab
  */
    async changeGiftStatus(value) {
      dispatch({
        type: Command.commonChange,
        payload: { paths: ['main', 'giftStatusTab'], value: value },
      });
    },

    /**
     *   获取我的道具
     */
    async propPage() {
      let {
        main: { form , propList = [] },
      } = getData();
      action.commonChange('main.isLoadingList', true);
      const res = await api.iplandBaseController.getMyProp(form);
      action.commonChange([{ paths: 'main.totalPages', value: (res as any).totalPages }]);
      if (form.pageNum == 0) {
        action.commonChange([
          { paths: 'main.propList', value: res?.content },
          { paths: 'main.totalPages', value: res?.totalPages },
        ]);
      } else {
        action.commonChange([{ paths: 'main.propList', value: propList.concat(res?.content) }]);
      }
      action.commonChange('main.isLoadingList', false);
    },

    /**
   * 查询下一页
   */
    async propNextPage() {
      let { form = {}, totalPages } = getData().main || {};
      let num = form.pageNum + 1;
      if (num == totalPages) return;
      dispatch({
        type: Command.commonChange,
        payload: {
          paths: 'main.form.pageNum',
          value: num,
        },
      });
      await this.propPage();
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageIBackpackMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
